package edu.sctu.springbootdemo.controller;

import edu.sctu.springbootdemo.entity.UserEntity;
import edu.sctu.springbootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/users")
    public List<UserEntity> findAll() {

        return userService.findAll();
    }

    @RequestMapping(value = "/users", method = RequestMethod.POST)
    public ModelAndView save(@RequestParam(name = "name")String name) {


        UserEntity userEntity = new UserEntity();
        userEntity.setName(name);
        userService.save(userEntity);

        ModelAndView model = new ModelAndView();
        model.addObject("message", "添加用户成功！");
        model.setViewName("/success");

        return model;
    }

    @RequestMapping(value = "/addUser")
    public ModelAndView addUser(){

        ModelAndView model = new ModelAndView();
        model.setViewName("/addUser");
        return model;
    }
}
